﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation.Results;
using SharpArch.Core.CommonValidator;
using IValidator = FluentValidation.IValidator;

namespace Peak.Web.Controllers.Models
{
  public class ModelBase : IValidatable
  {
    private readonly Func<IValidator> _validator;

    public ModelBase(Func<IValidator> validator)
    {
      _validator = validator;
    }

    public bool IsValid()
    {
      return ValidationResults().Count == 0;
    }

    public ICollection<IValidationResult> ValidationResults()
    {
      List<IValidationResult> validationResults = new List<IValidationResult>();
      if (_validator == null)
        return validationResults;

      ValidationResult result = _validator().Validate(this);
      if (result.Errors.Count > 0)
      {
        foreach (ValidationFailure error in result.Errors)
        {
          validationResults.Add(new PeakValidationResult(error, typeof(TaskModel)));
        }
      }
      return validationResults;
    }   
  }
}
